package com.sky.sd.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mysql.cj.util.StringUtils;
import com.sky.sd.common.web.response.Result;
import com.sky.sd.common.web.response.ResultType;
import com.sky.sd.system.bean.dto.LoginUser;
import com.sky.sd.system.bean.dto.UserDto;
import com.sky.sd.system.bean.po.User;
import com.sky.sd.system.bean.vo.*;
import com.sky.sd.system.service.IUserService;
import com.sky.sd.system.utils.LoginUserHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author 弓振
 * @since 2020-06-30
 */
@RestController
@RequestMapping("/system/user")
@Slf4j
@Validated
@Api(tags = "系统用户模块")
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/me")
    public Result<LoginUser> userMe() {
        LoginUser loginUser = LoginUserHolder.getLoginUser(SecurityContextHolder.getContext().getAuthentication());
        if(loginUser == null) {
            return Result.fail(ResultType.UNAUTHORIZED.getCode(), "获取用户失败", null);
        } else {
            return Result.success(loginUser);
        }
    }

    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", dataType = "int", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", example = "10"),
            @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "string", paramType = "query", example = ""),
            @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "string", paramType = "query", example = ""),
            @ApiImplicitParam(name = "nickName", value = "昵称", dataType = "string", paramType = "query", example = ""),
            @ApiImplicitParam(name = "userName", value = "登陆名", dataType = "string", paramType = "query", example = "")
    })
    @ApiOperation(value = "查询用户")
    public Result listUser(UserSearchVo vo) {
        IPage<UserDto> page = userService.listUserPage(vo);
        return Result.success(page);
    }

    @GetMapping("/check/code")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "登陆名", dataType = "string", paramType = "query", example = "",required = true)
    })
    @ApiOperation(value = "用户登录名是否存在")
    public Result check(UserCheckVo vo) {
        int cnt = userService.count(Wrappers.<User>lambdaQuery()
                .ne(!StringUtils.isNullOrEmpty(vo.getId()), User::getId, vo.getId()).eq(User::getUserName, vo.getUserName()));
        return Result.success(cnt);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增用户")
    public Result addUser(@RequestBody UserAddVo vo) {
        userService.add(vo);
        return Result.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改用户")
    public Result updateUser(@RequestBody UserUpdateVo vo) {
        userService.update(vo);
        return Result.success();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除用户")
    public Result deleteUser(@RequestBody DeleteVo vo) {
        userService.delete(vo);
        return Result.success();
    }

    @GetMapping("/ofRoles")
    @ApiOperation(value = "查询用户关联的角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", dataType = "int", paramType = "query", example = "",required = true)
    })
    public Result userOfRoles(@RequestParam("id") String userId) {
        Set<String> roleIds = userService.userOfRoles(userId);
        return Result.success(roleIds);
    }

    @PostMapping("/toRoles")
    @ApiOperation(value = "用户分配角色")
    public Result userToRoles(@RequestBody UserToRolesVo vo) {
        userService.userToRoles(vo);
        return Result.success();
    }
}

